﻿#include "AmapViewImpl.h"

#include "EAGLView.h"

#import <MAMapKit/MAMapKit.h>
#import <AMapLocationKit/AMapLocationManager.h>

#define AmapViewWrapper ((UIAmapViewWrapper*)m_pAmapWrapper)

@interface UIAmapViewWrapper : NSObject<MAMapViewDelegate, AMapLocationManagerDelegate>

@property(nonatomic, retain) MAMapView* mAmapView;

@property(nonatomic, retain) AMapLocationManager* mLocManager;

@property(nonatomic, retain) UIButton* mZoominBt;//放大按钮

@property(nonatomic, retain) UIButton* mZoomoutBt;//缩小按钮

@property(nonatomic, retain) UIButton* mLocationBt;//定位按钮

@property(nonatomic, assign) float mfZoomLevel;//缩放比例

@property(nonatomic, assign) float mfLat;//纬度

@property(nonatomic, assign) float mfLng;//经度

@property(nonatomic, assign) BOOL mbAmapLoaded;//地图是否加载成功

@property(nonatomic, assign) BOOL mbLoc;//是否定位

@property(nonatomic, strong) NSMutableDictionary* _MarkAnViews;

@property(nonatomic, retain) MAPointAnnotation* mLocationAn;

@end

@implementation UIAmapViewWrapper


+ (instancetype)amapWrapper
{
    return [[[self alloc] init] autorelease];
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.mAmapView = nil;
        
        self.mLocManager = nil;
        
        self.mZoominBt = nil;
        self.mZoomoutBt = nil;
        self.mLocationBt = nil;
        
        self.mLocationAn = nil;
        
        self.mfZoomLevel = 10.0;
        
        self.mbLoc = NO;
        
        self.mfLat = 39.9;
        self.mfLng = 116.3;
        
        self._MarkAnViews = [[NSMutableDictionary alloc] init];
        
        self.mbAmapLoaded = NO;
        
        [self setupAMapView];
    }
    return self;
}

- (void)dealloc
{
    [self.mLocManager stopUpdatingLocation];
    [self.mLocManager setDelegate:nil];
    [self.mLocManager release];
    
    [self.mAmapView removeAnnotation : self.mLocationAn];
    [self.mLocationAn release];
    
    [self.mZoominBt removeFromSuperview];
    [self.mZoominBt release];
    
    [self.mZoomoutBt removeFromSuperview];
    [self.mZoomoutBt release];
    
    [self.mLocationBt removeFromSuperview];
    [self.mLocationBt release];
    
    [self clearMarkers];
    
    [self.mAmapView release];
    
    [self._MarkAnViews removeAllObjects];
    [self._MarkAnViews release];
    
    [super dealloc];
}

- (void)setFrameWith_X:(float)x Y:(float)y Width:(float)width Height:(float)height
{
    if (!self.mAmapView)
    {
        [self setupAMapView];
    }
    
    CGFloat scale = [[UIScreen mainScreen] scale];
    float eightW = s_dip_to_px(80) / scale;
    float twoW = s_dip_to_px(20) / scale;
    
    CGRect newFrame = CGRectMake(x, y, width, height);
    [self.mAmapView setFrame:newFrame];
    
    CGRect zoominFrame = CGRectMake(width - twoW - eightW, height - eightW - eightW - twoW, eightW, eightW);
    [self.mZoominBt setFrame:zoominFrame];
    
    CGRect zoomoutFrame = CGRectMake(width - twoW - eightW, height - eightW - twoW, eightW, eightW);
    [self.mZoomoutBt setFrame:zoomoutFrame];
    
    CGRect locationFrame = CGRectMake(width - twoW - eightW, twoW, eightW, eightW);
    [self.mLocationBt setFrame:locationFrame];
    
    if (!self.mbAmapLoaded)
    {
        self.mbAmapLoaded = YES;
    
        [self.mAmapView setZoomLevel:self.mfZoomLevel];
    
        [self.mAmapView setCenterCoordinate:CLLocationCoordinate2DMake(self.mfLat, self.mfLng)];
    
        [self.mAmapView addAnnotation:self.mLocationAn];
        
        if (self.mbLoc)
        {
            [self.mAmapView setZoomLevel:16.0];
            
            [self.mLocationAn setCoordinate:CLLocationCoordinate2DMake(self.mfLat, self.mfLng)];
        }
        
        if ([self._MarkAnViews count] > 0)
        {
            for(id key in self._MarkAnViews.allKeys)
            {
                MAPinAnnotationView* newpinView = [self._MarkAnViews objectForKey:key];
                if (newpinView != nil)
                {
                    [self.mAmapView addAnnotation:newpinView.annotation];
                }
            }
        }
    }
}

- (void)setupAMapView
{
    if (!self.mAmapView)
    {
        self.mAmapView = [[MAMapView alloc] init];
        [self.mAmapView setDelegate:self];
    }
    if (!self.mAmapView.superview)
    {
        EAGLView * eaglview = [EAGLView sharedEGLView];
        [eaglview addSubview:self.mAmapView];
        [eaglview bringSubviewToFront:self.mAmapView];
    }
    
    if (!self.mLocManager)
    {
        self.mLocManager = [[AMapLocationManager alloc]init];
        [self.mLocManager setDelegate:self];
        //设置不允许系统暂停定位
        [self.mLocManager setPausesLocationUpdatesAutomatically:NO];
        //设置允许在后台定位
        [self.mLocManager setAllowsBackgroundLocationUpdates:YES];
        //设置期望定位精度
        [self.mLocManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
    }
    
    if (!self.mZoominBt)
    {
        self.mZoominBt = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.mZoominBt addTarget:self action:@selector(zoominbtnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.mZoominBt setImage:[UIImage imageNamed:@"zoomin_selected2d"] forState:UIControlStateNormal];
    }
    if (!self.mZoominBt.superview)
    {
        [self.mAmapView addSubview:self.mZoominBt];
    }
    
    if (!self.mZoomoutBt)
    {
        self.mZoomoutBt = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.mZoomoutBt addTarget:self action:@selector(zoomoutbtnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.mZoomoutBt setImage:[UIImage imageNamed:@"zoomout_selected2d"] forState:UIControlStateNormal];
    }
    if (!self.mZoomoutBt.superview)
    {
        [self.mAmapView addSubview:self.mZoomoutBt];
    }
    
    if (!self.mLocationBt)
    {
        self.mLocationBt = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.mLocationBt addTarget:self action:@selector(locationbtnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.mLocationBt setImage:[UIImage imageNamed:@"location_selected2d"] forState:UIControlStateNormal];
    }
    if (!self.mLocationBt.superview)
    {
        [self.mAmapView addSubview:self.mLocationBt];
    }
    
    if (!self.mLocationAn)
    {
        self.mLocationAn = [[MAPointAnnotation alloc] init];
        [self.mLocationAn setTitle:@"我的位置"];
        [self.mLocationAn setSubtitle:@""];
    }
}

- (void)zoominbtnPressed:(id)sender
{
    if (self.mAmapView != nil)
    {
        double level = [self.mAmapView zoomLevel];
        level += 0.5;
        [self.mAmapView setZoomLevel:level animated:YES];
    }
}

- (void)zoomoutbtnPressed:(id)sender
{
    if (self.mAmapView != nil)
    {
        double level = [self.mAmapView zoomLevel];
        level -= 0.5;
        [self.mAmapView setZoomLevel:level animated:YES];
    }
}

- (void)setMapCenterLat:(float)lat Lng:(float)lng
{
    self.mbLoc = NO;
    
    self.mfLat = lat;
    self.mfLng = lng;
    
    if (self.mbAmapLoaded)
    {
        [self.mAmapView setCenterCoordinate:CLLocationCoordinate2DMake(lat, lng)];
        [self.mAmapView setZoomLevel:[self.mAmapView zoomLevel]];
    }
}

- (void)locationbtnPressed:(id)sender
{
    [self startLocation];
}

- (void)location
{
    if (self.mLocManager)
    {
        self.mbLoc = YES;
        [self startLocation];
    }
}

- (void)startLocation
{
    if (self.mLocManager)
    {
        [self.mLocManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error)
         {
             if (error != nil && error.code == AMapLocationErrorLocateFailed)
             {
                 //定位错误：此时location和regeocode没有返回值，不进行annotation的添加
                 AmapViewImpl::onAmapLocationEnd(self, -2, 0.0, 0.0, "", "", "", "", "", "", "", "");
                 return;
             }
             else if (error != nil
                      && (error.code == AMapLocationErrorReGeocodeFailed
                          || error.code == AMapLocationErrorTimeOut
                          || error.code == AMapLocationErrorCannotFindHost
                          || error.code == AMapLocationErrorBadURL
                          || error.code == AMapLocationErrorNotConnectedToInternet
                          || error.code == AMapLocationErrorCannotConnectToHost))
             {
                 //逆地理错误：在带逆地理的单次定位中，逆地理过程可能发生错误，此时location有返回值，regeocode无返回值，进行annotation的添加
                 AmapViewImpl::onAmapLocationEnd(self, -1, location.coordinate.latitude, location.coordinate.longitude, "", "", "", "", "", "", "", "");
                 return;
             }
             else
             {
                 //没有错误：location有返回值，regeocode是否有返回值取决于是否进行逆地理操作，进行annotation的添加
                 AmapViewImpl::onAmapLocationEnd(self, 0, location.coordinate.latitude, location.coordinate.longitude, [regeocode.formattedAddress UTF8String], [regeocode.province UTF8String], [regeocode.city UTF8String], [regeocode.district UTF8String], [regeocode.street UTF8String], [regeocode.number UTF8String], [regeocode.POIName UTF8String], [regeocode.adcode UTF8String]);
             }
             
             //修改Location显示内容
             if (location)
             {
                 self.mfLat = location.coordinate.latitude;
                 self.mfLat = location.coordinate.longitude;
                 if (self.mbAmapLoaded)
                 {
                     [self.mAmapView setCenterCoordinate:location.coordinate];
                     [self.mAmapView setZoomLevel:16.0];
                     
                     [self.mLocationAn setCoordinate:location.coordinate];
                 }
             }
             
             if (regeocode)
             {
                 [self.mLocationAn setTitle:regeocode.formattedAddress];
             }
             else
             {
                 [self.mLocationAn setTitle:@"我的位置"];
             }
         }];
    }
}

- (void)stopLocation
{
    if (self.mLocManager)
    {
        [self.mLocManager stopUpdatingLocation];
    }
}

- (void)setMapZoomLevel:(float)level
{
    self.mfZoomLevel = level;
    
    if (self.mbAmapLoaded)
    {
        [self.mAmapView setZoomLevel:level];
    }
}

- (void)addMarkerTag:(NSString*)tag Image: (NSString*) imgurl Draggable:(BOOL) draggable Title:(NSString*) title Content:(NSString*)content Lat:(double)lat Lng:(double) lng
{
    MAPointAnnotation* pointAn = [[MAPointAnnotation alloc]init];
    [pointAn setCoordinate:CLLocationCoordinate2DMake(lat, lng)];
    [pointAn setTitle:title];
    [pointAn setSubtitle:content];
    
    MAPinAnnotationView* pinAnView = [[MAPinAnnotationView alloc] init];
    [pinAnView setAnnotation:pointAn];
    [pinAnView setDraggable:draggable];
    [pinAnView setImage:[UIImage imageNamed:imgurl]];
    
    [self._MarkAnViews setObject:pinAnView forKey:tag];
    
    if (self.mbAmapLoaded)
    {
        [self.mAmapView addAnnotation:pointAn];
    }
}

- (void)deleteMarkerTag:(NSString*)tag
{
    MAPinAnnotationView* pinAnView = [self._MarkAnViews objectForKey:tag];
    if (pinAnView != nil)
    {
        [self.mAmapView removeAnnotation:pinAnView.annotation];
        [self._MarkAnViews removeObjectForKey:tag];
    }
}

- (void)clearMarkers
{
    for(id key in self._MarkAnViews.allKeys)
    {
        MAPinAnnotationView* newpinView = [self._MarkAnViews objectForKey:key];
        if (newpinView != nil)
        {
            [self.mAmapView removeAnnotation:newpinView.annotation];
        }
    }
    
    [self._MarkAnViews removeAllObjects];
}

- (void)setVisible:(bool)visible
{
    self.mAmapView.hidden = !visible;
}

#pragma mark - AMapLocationManagerDelegate
/**
 *  当定位发生错误时，会调用代理的此方法。
 *
 *  @param manager 定位 AMapLocationManager 类。
 *  @param error 返回的错误，参考 CLError 。
 */
- (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error
{
    [self stopLocation];
    
    AmapViewImpl::onAmapLocationEnd(self, -2, 0.0, 0.0, "", "", "", "", "", "", "", "");
}

#pragma mark - MAMapViewDelegate

/**
 * @brief 单击地图底图调用此接口
 * @param mapView    地图View
 * @param coordinate 点击位置经纬度
 */
- (void)mapView:(MAMapView *)mapView didSingleTappedAtCoordinate:(CLLocationCoordinate2D)coordinate
{
    AmapViewImpl::onAmapClicked(self, coordinate.latitude, coordinate.longitude);
}

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *pinIndetifier = @"pinIndetifier";
        MAPinAnnotationView *annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pinIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinIndetifier];
        }
        
        annotationView.canShowCallout = YES;
        annotationView.animatesDrop = NO;
        
        if (annotation == self.mLocationAn)
        {
            annotationView.draggable = NO;
            annotationView.pinColor = MAPinAnnotationColorGreen;
        }
        else
        {
            for(id key in self._MarkAnViews.allKeys)
            {
                MAPinAnnotationView* newpinView = [self._MarkAnViews objectForKey:key];
                if (newpinView != nil && (id<MAAnnotation>)[newpinView annotation] == annotation)
                {
                    if ([newpinView image] != nil)
                    {
                        [annotationView setImage:[newpinView image]];
                    }
                    else
                    {
                        annotationView.pinColor = MAPinAnnotationColorRed;
                    }
                    annotationView.draggable = newpinView.draggable;
                    
                    break;
                }
            }
        }
        
        return annotationView;
    }
    
    return nil;
}

/**
 * @brief 当选中一个annotation views时调用此接口
 * @param mapView 地图View
 * @param view 选中的annotationView
 */
- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view
{
    for(id key in self._MarkAnViews.allKeys)
    {
        MAPinAnnotationView* newpinView = [self._MarkAnViews objectForKey:key];
        if (newpinView != nil && (id<MAAnnotation>)[newpinView annotation] == view.annotation)
        {
            AmapViewImpl::onAmapMarkerClick(self, [key UTF8String]);
            break;
        }
    }
}

/**
 * @brief 拖动annotation view时view的状态变化，ios3.2以后支持
 * @param mapView 地图View
 * @param view annotation view
 * @param newState 新状态
 * @param oldState 旧状态
 */
- (void)mapView:(MAMapView *)mapView annotationView:(MAAnnotationView *)view didChangeDragState:(MAAnnotationViewDragState)newState fromOldState:(MAAnnotationViewDragState)oldState
{
    NSString* newKey = nil;
    for(id key in self._MarkAnViews.allKeys)
    {
        MAPinAnnotationView* newpinView = [self._MarkAnViews objectForKey:key];
        if (newpinView != nil && (id<MAAnnotation>)[newpinView annotation] == view.annotation)
        {
            newKey = key;
            break;
        }
    }
    if (newKey != nil)
    {
        if (newState == MAAnnotationViewDragStateStarting)
        {
            AmapViewImpl::onAmapMarkerDragStart(self, [newKey UTF8String], [[view annotation] coordinate].latitude, [[view annotation] coordinate].longitude);
        }
        else if (newState == MAAnnotationViewDragStateDragging)
        {
            AmapViewImpl::onAmapMarkerDrag(self, [newKey UTF8String], [[view annotation] coordinate].latitude, [[view annotation] coordinate].longitude);
        }
        else if (oldState == MAAnnotationViewDragStateEnding && newState == MAAnnotationViewDragStateNone)
        {
            AmapViewImpl::onAmapMarkerDragEnd(self, [newKey UTF8String], [[view annotation] coordinate].latitude, [[view annotation] coordinate].longitude);
        }
    }
}

/**
 * @brief 标注view的calloutview整体点击时调用此接口
 * @param mapView 地图的view
 * @param view calloutView所属的annotationView
 */
- (void)mapView:(MAMapView *)mapView didAnnotationViewCalloutTapped:(MAAnnotationView *)view
{
    for(id key in self._MarkAnViews.allKeys)
    {
        MAPinAnnotationView* newpinView = [self._MarkAnViews objectForKey:key];
        if (newpinView != nil && (id<MAAnnotation>)[newpinView annotation] == view.annotation)
        {
            AmapViewImpl::onAmapMarkerInfoWindowClick(self, [key UTF8String]);
            break;
        }
    }
}

@end


static std::map<UIAmapViewWrapper*, AmapViewImpl*> s_AMapViewImpls;
AmapViewImpl::AmapViewImpl(AmapView *mapView) : m_pAmapView(mapView), m_pAmapWrapper([UIAmapViewWrapper amapWrapper])
{
    [AmapViewWrapper retain];
	s_AMapViewImpls[AmapViewWrapper] = this;
}

AmapViewImpl::~AmapViewImpl()
{
    clearMarkers();
    
    [AmapViewWrapper release];
    s_AMapViewImpls.erase(AmapViewWrapper);
    
    m_pAmapWrapper = NULL;
}

void AmapViewImpl::setMapCenter(double lat, double lng)
{
    [AmapViewWrapper setMapCenterLat:lat Lng:lng];
}

void AmapViewImpl::startLocation()
{
    [AmapViewWrapper location];
}

void AmapViewImpl::setMapZoomLevel(float level)
{
    [AmapViewWrapper setMapZoomLevel:level];
}

void AmapViewImpl::update(float dt)
{
    DRect cRect = m_pAmapView->convertRectToWorldSpace(m_pAmapView->getBounds());
    cRect.origin.x = s_dip_to_px(cRect.origin.x);
    cRect.origin.y = s_dip_to_px(cRect.origin.y);
    cRect.size.width = s_dip_to_px(cRect.size.width);
    cRect.size.height = s_dip_to_px(cRect.size.height);
    
    CGFloat scale = [[UIScreen mainScreen] scale];
    CGFloat x = cRect.origin.x/scale;
    CGFloat y = cRect.origin.y/scale;
    CGFloat width = cRect.size.width/scale;
    CGFloat height = cRect.size.height/scale;
    
    [AmapViewWrapper setFrameWith_X:x Y:y Width:width Height:height];
}

void AmapViewImpl::addMarker(AmapMarker* marker)
{
    marker->retain();

    [AmapViewWrapper addMarkerTag: [NSString stringWithUTF8String:crossapp_format_string("%p", marker).c_str()] Image:[NSString stringWithUTF8String:marker->getImg().c_str()] Draggable:marker->getDraggable() Title:[NSString stringWithUTF8String:marker->getTitle().c_str()] Content:[NSString stringWithUTF8String:marker->getContent().c_str()] Lat:marker->getLat() Lng:marker->getLng()];
    
    s_AmapMarkers[crossapp_format_string("%p", marker)] = marker;
}

void AmapViewImpl::deleteMarker(AmapMarker* marker)
{
    std::map<string, AmapMarker*>::iterator it = s_AmapMarkers.find(crossapp_format_string("%p", marker));
    if (it != s_AmapMarkers.end())
    {
        [AmapViewWrapper deleteMarkerTag:[NSString stringWithUTF8String:crossapp_format_string("%p", marker).c_str()]];
        s_AmapMarkers.erase(it);
        CC_SAFE_RELEASE_NULL(marker);
    }
}

std::map<string, AmapMarker*>& AmapViewImpl::getAllMarkers()
{
    return s_AmapMarkers;
}

void AmapViewImpl::clearMarkers()
{
    for (std::map<string, AmapMarker*>::iterator it = s_AmapMarkers.begin(); it != s_AmapMarkers.end(); ++it)
    {
        CC_SAFE_RELEASE_NULL(it->second);
    }
    
    s_AmapMarkers.clear();
    
    [AmapViewWrapper clearMarkers];
}

void AmapViewImpl::setVisible(bool visible)
{
    [AmapViewWrapper setVisible:visible];
}

void AmapViewImpl::onAmapClicked(void* amapwrapper, double lat, double lng)
{
	std::map<UIAmapViewWrapper*, AmapViewImpl*>::iterator it = s_AMapViewImpls.find((UIAmapViewWrapper*)amapwrapper);
	if (it != s_AMapViewImpls.end())
	{
		AmapView* ampView = it->second->m_pAmapView;
		if (ampView && ampView->getAmapViewDelegate())
		{
			ampView->getAmapViewDelegate()->onAmapClicked(lat, lng);
		}
	}
}

void AmapViewImpl::onAmapLocationEnd(void* amapwrapper, int code, double lat, double lng, const string& formatAddr, const string& province, const string& city, const string& district, const string& street, const string& number, const string& poi, const string& adcode)
{
    std::map<UIAmapViewWrapper*, AmapViewImpl*>::iterator it = s_AMapViewImpls.find((UIAmapViewWrapper*)amapwrapper);
    if (it != s_AMapViewImpls.end())
    {
        AmapView* ampView = it->second->m_pAmapView;
        if (ampView && ampView->getAmapViewDelegate())
        {
            ampView->getAmapViewDelegate()->onAmapLocationEnd(code, lat, lng, formatAddr, province, city, district, street, number, poi, adcode);
        }
    }
}

void AmapViewImpl::onAmapMarkerDragStart(void* amapwrapper, const string& markertag, double lat, double lng)
{
    std::map<UIAmapViewWrapper*, AmapViewImpl*>::iterator it = s_AMapViewImpls.find((UIAmapViewWrapper*)amapwrapper);
    if (it != s_AMapViewImpls.end())
    {
        std::map<string, AmapMarker*>::iterator mIt = it->second->getAllMarkers().find(markertag);
        if (mIt != it->second->getAllMarkers().end())
        {
            AmapView* ampView = it->second->m_pAmapView;
            if (ampView && ampView->getAmapViewDelegate())
            {
                ampView->getAmapViewDelegate()->onAmapMarkerDragStart(mIt->second, lat, lng);
            }
        }
    }
}

void AmapViewImpl::onAmapMarkerDrag(void* amapwrapper, const string& markertag, double lat, double lng)
{
    std::map<UIAmapViewWrapper*, AmapViewImpl*>::iterator it = s_AMapViewImpls.find((UIAmapViewWrapper*)amapwrapper);
    if (it != s_AMapViewImpls.end())
    {
        std::map<string, AmapMarker*>::iterator mIt = it->second->getAllMarkers().find(markertag);
        if (mIt != it->second->getAllMarkers().end())
        {
            AmapView* ampView = it->second->m_pAmapView;
            if (ampView && ampView->getAmapViewDelegate())
            {
                ampView->getAmapViewDelegate()->onAmapMarkerDrag(mIt->second, lat, lng);
            }
        }
    }
}

void AmapViewImpl::onAmapMarkerDragEnd(void* amapwrapper, const string& markertag, double lat, double lng)
{
    std::map<UIAmapViewWrapper*, AmapViewImpl*>::iterator it = s_AMapViewImpls.find((UIAmapViewWrapper*)amapwrapper);
    if (it != s_AMapViewImpls.end())
    {
        std::map<string, AmapMarker*>::iterator mIt = it->second->getAllMarkers().find(markertag);
        if (mIt != it->second->getAllMarkers().end())
        {
            AmapView* ampView = it->second->m_pAmapView;
            if (ampView && ampView->getAmapViewDelegate())
            {
                ampView->getAmapViewDelegate()->onAmapMarkerDragEnd(mIt->second, lat, lng);
            }
        }
    }
}

void AmapViewImpl::onAmapMarkerClick(void* amapwrapper, const string& markertag)
{
    std::map<UIAmapViewWrapper*, AmapViewImpl*>::iterator it = s_AMapViewImpls.find((UIAmapViewWrapper*)amapwrapper);
    if (it != s_AMapViewImpls.end())
    {
        std::map<string, AmapMarker*>::iterator mIt = it->second->getAllMarkers().find(markertag);
        if (mIt != it->second->getAllMarkers().end())
        {
            AmapView* ampView = it->second->m_pAmapView;
            if (ampView && ampView->getAmapViewDelegate())
            {
                ampView->getAmapViewDelegate()->onAmapMarkerClick(mIt->second);
            }
        }
    }
}

void AmapViewImpl::onAmapMarkerInfoWindowClick(void* amapwrapper, const string& markertag)
{
    std::map<UIAmapViewWrapper*, AmapViewImpl*>::iterator it = s_AMapViewImpls.find((UIAmapViewWrapper*)amapwrapper);
    if (it != s_AMapViewImpls.end())
    {
        std::map<string, AmapMarker*>::iterator mIt = it->second->getAllMarkers().find(markertag);
        if (mIt != it->second->getAllMarkers().end())
        {
            AmapView* ampView = it->second->m_pAmapView;
            if (ampView && ampView->getAmapViewDelegate())
            {
                ampView->getAmapViewDelegate()->onAmapMarkerInfoWindowClick(mIt->second);
            }
        }
    }
}

